package com.example.studyonlineapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;


import com.example.studyonlineapplication.fragments.NoneFragment;
import com.example.studyonlineapplication.fragments.NoteFragment;
import com.example.studyonlineapplication.utils.MainFragmentPageAdapter;

import java.util.ArrayList;

public class AppActivityMain extends AppCompatActivity implements View.OnClickListener {
    ViewPager2 viewPager;
    ImageButton mainPageBtn,studyPageBtn,notePageBtn,myPageBtn,curentBtn;
    ArrayList<Fragment> fragments =new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        init();

    }
    private void init(){
        mainPageBtn = findViewById(R.id.mainPageBtn);
        studyPageBtn = findViewById(R.id.studyPageBtn);
        notePageBtn = findViewById(R.id.notoPageBtn);
        myPageBtn = findViewById(R.id.myPageBtn);


        mainPageBtn.setOnClickListener(this);
        studyPageBtn.setOnClickListener(this);
        notePageBtn.setOnClickListener(this);
        myPageBtn.setOnClickListener(this);
        mainPageBtn.setSelected(true);
        curentBtn = mainPageBtn;

        viewPager = findViewById(R.id.viewPage);

        fragments.add(new NoneFragment());
        fragments.add(new NoneFragment());
        fragments.add(new NoteFragment());
        fragments.add(new NoneFragment());


        MainFragmentPageAdapter adapter = new MainFragmentPageAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        viewPager.setAdapter(adapter);

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                curentBtn.setSelected(false);
                switch (position){
                    case 0:
                        curentBtn = mainPageBtn;
                        break;
                    case 1:
                        curentBtn = studyPageBtn;
                        break;
                    case 2:
                        curentBtn = notePageBtn;
                        break;
                    case 3:
                        curentBtn = myPageBtn;
                        break;
                }
                curentBtn.setSelected(true);
            }

        });

    }


    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        curentBtn.setSelected(false);
        if (viewId == R.id.mainPageBtn){
            curentBtn = mainPageBtn;
            viewPager.setCurrentItem(0);
        }
        else if(viewId == R.id.studyPageBtn){
            curentBtn = studyPageBtn;
            viewPager.setCurrentItem(1);
        } else if (viewId == R.id.notoPageBtn) {
            curentBtn = notePageBtn;
            viewPager.setCurrentItem(2);
        } else{
            curentBtn = myPageBtn;
            viewPager.setCurrentItem(3);
        }
        curentBtn.setSelected(true);
    }
}
